package com.ruoyi.basic.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.mapper.BasicUserMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basic.mapper.BasicDutyMapper;
import com.ruoyi.basic.domain.BasicDuty;
import com.ruoyi.basic.service.IBasicDutyService;
import com.ruoyi.common.core.text.Convert;

/**
 * 行政职务Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-06
 */
@Service
public class BasicDutyServiceImpl extends ServiceImpl<BasicDutyMapper, BasicDuty> implements IBasicDutyService
{
    @Autowired
    private BasicUserMapper userMapper;

    /**
     * 查询行政职务
     * 
     * @param dutyId 行政职务主键
     * @return 行政职务
     */
    @Override
    public BasicDuty selectBasicDutyByDutyId(Long dutyId)
    {
        return baseMapper.selectBasicDutyByDutyId(dutyId);
    }

    /**
     * 查询行政职务列表
     * 
     * @param basicDuty 行政职务
     * @return 行政职务
     */
    @Override
    public List<BasicDuty> selectBasicDutyList(BasicDuty basicDuty)
    {
        return baseMapper.selectBasicDutyList(basicDuty);
    }

    /**
     * 新增行政职务
     * 
     * @param basicDuty 行政职务
     * @return 结果
     */
    @Override
    public int insertBasicDuty(BasicDuty basicDuty)
    {
        basicDuty.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(basicDuty);
    }

    /**
     * 修改行政职务
     * 
     * @param basicDuty 行政职务
     * @return 结果
     */
    @Override
    public int updateBasicDuty(BasicDuty basicDuty)
    {
        basicDuty.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(basicDuty);
    }

    /**
     * 批量删除行政职务
     * 
     * @param dutyIds 需要删除的行政职务主键
     * @return 结果
     */
    @Override
    public int deleteBasicDutyByDutyIds(String dutyIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(dutyIds)));
    }

    /**
     * 删除行政职务信息
     * 
     * @param dutyId 行政职务主键
     * @return 结果
     */
    @Override
    public int deleteBasicDutyByDutyId(Long dutyId)
    {
        return baseMapper.deleteById(dutyId);
    }

    /**
     * 导入员工行政职务
     *
     * @param dutyList 员工行政职务列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importDuty(List<BasicDuty> dutyList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(dutyList) || dutyList.size() == 0)
        {
            throw new ServiceException("导入员工行政职务数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BasicDuty duty : dutyList)
        {
            int row = 1;
            try
            {
                if(StringUtils.isEmpty(duty.getDutyName())){
                    continue;
                }
                //获取用户信息
                BasicUser user = userMapper.selectOne(new LambdaQueryWrapper<BasicUser>()
                        .eq(BasicUser::getIdCard, duty.getIdCard()).last("limit 1"));
                if(StringUtils.isNotNull(user)) {
                    duty.setUserId(user.getId());
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第 " + row + "行导入失败，员工" + duty.getUserName() + " 不存在");
                    continue;
                }

                BasicDuty info = baseMapper.selectOne(new LambdaQueryWrapper<BasicDuty>()
                        .eq(BasicDuty::getDutyName, duty.getDutyName())
                        .eq(BasicDuty::getDutyLevel, duty.getDutyLevel())
                        .eq(BasicDuty::getStartTime, duty.getStartTime())
                        .eq(BasicDuty::getUserId, duty.getUserId()).last("limit 1"));
                if (StringUtils.isNull(info))
                {
                    duty.setCreateBy(operName);
                    this.insertBasicDuty(duty);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + duty.getUserName() + "行政职务 " + duty.getDutyName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    duty.setDutyId(info.getDutyId());
                    duty.setUpdateBy(operName);
                    this.updateBasicDuty(duty);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + duty.getUserName() + "行政职务 " + duty.getDutyName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、员工 " + duty.getUserName() + "行政职务 " + duty.getDutyName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工 " + duty.getUserName() + " 行政职务导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
            finally {
                row++;
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
